package com.hnucm.cooklife.ui.activity;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.graphics.ColorUtils;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.appbar.AppBarLayout;
import com.hnucm.cooklife.R;
import com.hnucm.cooklife.adapter.DishStepAdapter;
import com.hnucm.cooklife.adapter.DishUsageAdapter;
import com.hnucm.cooklife.base.BaseActivity;
import com.hnucm.cooklife.base.Utils;
import com.hnucm.cooklife.base.utils.LoginUtils;
import com.hnucm.cooklife.bean.base.Event;
import com.hnucm.cooklife.bean.db.DishDetail;
import com.hnucm.cooklife.bean.me.ToAddCollectData;
import com.hnucm.cooklife.contract.dishdetail.Contract;
import com.hnucm.cooklife.custom.NestCollapsingToolbarLayout;
import com.hnucm.cooklife.custom.UsageItemDecoration;
import com.hnucm.cooklife.presenter.detail.DishDetailPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class DishDetailActivity extends BaseActivity<Contract.IDishDetailView, DishDetailPresenter> implements Contract.IDishDetailView {
    private Context mContext;
    private DishUsageAdapter mDishUsageAdapter;
    private DishStepAdapter mDishStepAdapter;
    private DishDetail mDishDetail = new DishDetail();

    private ObjectAnimator mObjectAnimator;

    @BindView(R.id.detail_name)
    TextView mDetailName;

    @BindView(R.id.detail_author)
    TextView mDetailAuthor;

    @BindView(R.id.detail_desc)
    TextView mDetailDesc;

    @BindView(R.id.detail_usage)
    RecyclerView mDetailUsage;

    @BindView(R.id.detail_step)
    RecyclerView mDetailStep;

    @BindView(R.id.llSmallAuthor)
    LinearLayout mSmallAuthor;

    @BindView(R.id.toolbarLayout)
    NestCollapsingToolbarLayout mAppBarLayout;

    @BindView(R.id.rlTitle)
    ConstraintLayout mTitile;

    @BindView(R.id.ivBack)
    ImageView mBack;

    @BindView(R.id.small_header)
    ImageView mSmallHeader;

    @BindView(R.id.small_follow)
    TextView mSmallFollow;

    @BindView(R.id.ivShare)
    ImageView mWechat;

    @BindView(R.id.ivZone)
    ImageView mZone;

    @BindView(R.id.dish_pic)
    ImageView mDishPic;

    @BindView(R.id.detail_header)
    CircleImageView mDetailHeader;

    @BindView(R.id.item_list_collect)
    ImageView mCollect;

    @BindView(R.id.detail_follow)
    TextView mFoll;

    private int dishId;

    @OnClick(R.id.item_list_collect)
    public void onClick(){
        if(mCollect.isSelected() == false){
            mPresenter.collect(LoginUtils.getLoginId(),dishId);
        } else {
            mPresenter.unCollect(LoginUtils.getLoginId(),dishId);
        }

    }

    @OnClick(R.id.detail_follow)
    public void click(){
        if(mFoll.getText().equals("关注")){
            mFoll.setText("已关注");
            mFoll.setBackground(mContext.getDrawable(R.drawable.gray_shape));
        } else {
            mFoll.setText("关注");
            mFoll.setBackground(mContext.getDrawable(R.drawable.circle_shape));
        }
    }

    @Override
    public void onCollect(ToAddCollectData collectData, int dishId) {
//        mCollect.setBackgroundColor(Color.BLUE);
        mCollect.setSelected(true);
    }

    @Override
    public void onUnCollect(ToAddCollectData collectData, int dishId) {
        mCollect.setSelected(false);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_dishdetail;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        mContext = getApplicationContext();
        dishId = getIntent().getIntExtra("dishId", 0);
        initAnimator();
        initAdapter();
        initAppBar();
        initStatusBar();
        mPresenter.loadDishDetail(dishId);
        mPresenter.updateHistory(dishId, LoginUtils.getLoginId());
        callHis();
    }


    public void callHis(){
        Event event = new Event();
        event.target = Event.TARGET_HISTORY;
        event.type = Event.TYPE_UPDATE_HISTORY;
        event.data = String.valueOf(dishId);
        EventBus.getDefault().post(event);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().setStatusBarColor(Color.WHITE);
        }
        if (ColorUtils.calculateLuminance(Color.WHITE) >= 0.5) {
            // 设置状态栏中字体的颜色为黑色
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            // 跟随系统
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    private void initAnimator() {
        mObjectAnimator = ObjectAnimator.ofFloat(
                mSmallAuthor,"translationY",
                120f,0f
        );
        mObjectAnimator.setInterpolator(new AccelerateInterpolator());
        mObjectAnimator.setDuration(300);
    }

    private void initAppBar(){
        mAppBarLayout.setScrimsShowListener(new NestCollapsingToolbarLayout.OnScrimsShowListener() {
            @Override
            public void onScrimsShowChange(NestCollapsingToolbarLayout nestCollapsingToolbarLayout, boolean isScrimsShow) {
                if(isScrimsShow){
                    //把字体背景色设为白色，图标设为铂略
                    mTitile.setBackgroundColor(Color.WHITE);
                    mBack.setImageResource(R.drawable.ic_baseline_arrow_back_ios_24);
                    mWechat.setImageResource(R.drawable.ic_wechat_36);
                    mZone.setImageResource(R.drawable.ic_zone_36);
                    showSmallAuthor();
                } else {
                    //把字体背景色设为透明色
                    mTitile.setBackgroundColor(Color.TRANSPARENT);
                    mBack.setImageResource(R.drawable.ic_baseline_arrow_back_ios_white_24);
                    mWechat.setImageResource(R.drawable.ic_wechat_white_36);
                    mZone.setImageResource(R.drawable.ic_zone_white_36);
                    showSmallNot();

                    if(mObjectAnimator.isRunning()){
                        mObjectAnimator.cancel();
                    }
                }
            }
        });
    }

    private void showSmallAuthor(){
        mSmallAuthor.setVisibility(View.VISIBLE);
    }

    private void showSmallNot(){
        mSmallAuthor.setVisibility(View.GONE);
    }

    Drawable tindDrawable(Drawable drawable, ColorStateList colorStateList) {
        Drawable wrappeDrawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTintList(wrappeDrawable,colorStateList);
        return wrappeDrawable;
    }

    private void initAdapter() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        mDetailUsage.setLayoutManager(manager);
        LinearLayoutManager manager1 = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        mDetailStep.setLayoutManager(manager1);

        System.out.println(mDishDetail.stepList);
        mDishStepAdapter = new DishStepAdapter(mContext,mDishDetail.stepList);
        mDetailStep.setAdapter(mDishStepAdapter);
        mDishUsageAdapter  = new DishUsageAdapter(mContext, mDishDetail.originList);
        mDetailUsage.setAdapter(mDishUsageAdapter);
        mDetailUsage.addItemDecoration(new UsageItemDecoration());
    }

    @Override
    protected DishDetailPresenter createPresenter() {
        return new DishDetailPresenter();
    }



    @Override
    public void onLoadDishDetail(DishDetail dishDetail) {
        mDishDetail.originList.addAll(dishDetail.originList);
        mDishDetail.stepList.addAll(dishDetail.stepList);
        mDishUsageAdapter.setOriginList(mDishDetail.originList);
        mDishStepAdapter.setStepList(mDishDetail.stepList);

        mDetailName.setText(dishDetail.dishname);
        mDetailAuthor.setText(dishDetail.username);
        mDetailDesc.setText(dishDetail.dishdesc);

        Glide.with(mContext)
                .load(dishDetail.dishpic)
                .fitCenter()
                .centerCrop()
                .into(mDishPic);

        Glide.with(mContext)
                .load(dishDetail.icon)
                .fitCenter()
                .centerCrop()
                .into(mDetailHeader);

        Glide.with(mContext)
                .load(dishDetail.icon)
                .fitCenter()
                .centerCrop()
                .into(mSmallHeader);
    }

    @Override
    public void onRefreshDishDetail(DishDetail dishDetail) {

    }
    @Override
    public void onLoading() {
    }

    @Override
    public void onLoadingFailed() {

    }

    @Override
    public void onLoadSuccess() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(Event event){

    }
}
